package com.pankai.cn.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pankai.cn.entity.Role;
import com.pankai.cn.entity.User;
import com.pankai.cn.mapper.UserMapper;
import com.pankai.cn.service.UserService;
import com.pankai.cn.utils.SaltUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Wrapper;
import java.util.List;

/**
 * @author pankai
 * @create 2020-12-02 13:30
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void register(User user) {

        // 1. 生成随机盐
        String salt = SaltUtils.getSalt(8);
        // 2. 将随机盐保存到数据库
        user.setSalt(salt);
        // 3. 明文密码进行md5 + salt + hash散列加密
        Md5Hash md5Hash = new Md5Hash(user.getPassword(),salt,1024);
        user.setPassword(md5Hash.toHex());

        userMapper.insert(user);


    }

    @Override
    public User findUserByUserName(String userName) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",userName);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public User findRolesByUserName(String userName) {
        return userMapper.findRolesByUserName(userName);
    }
}
